import {getSessionStorage} from '@/utils'
import {StorageEnum} from '@/enums/storageEnum'
import {ChartEditStorage} from '@/store/modules/chartEditStore/chartEditStore.d'
import {useChartEditStore} from '@/store/modules/chartEditStore/chartEditStore'

const chartEditStore = useChartEditStore()

export interface ChartEditStorageType extends ChartEditStorage {
    id: string
}

// 根据路由 id 获取存储数据的信息
export const getSessionStorageInfo = (_id: string) => {
    const urlHash = document.location.hash
    const toPathArray = urlHash.split('/')
    let id = toPathArray && toPathArray[toPathArray.length - 1]
    if (_id) {
        id = _id
    }
    const storageList: ChartEditStorageType[] = getSessionStorage(
        StorageEnum.GO_CHART_STORAGE_LIST
    )
    if (storageList) {
        for (let i = 0; i < storageList.length; i++) {
            if (id.toString() === storageList[i]['id'].toString()) {
                const {editCanvasConfig, requestGlobalConfig, componentList} = storageList[i]
                chartEditStore.editCanvasConfig = editCanvasConfig
                chartEditStore.requestGlobalConfig = requestGlobalConfig
                chartEditStore.componentList = componentList
                return storageList[i]
            }
        }
    }
}
